package com.ewzj.bigdata.mvp.presenter;

import android.app.Application;

import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseListData;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.SearchResultBean;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.SearchResultContract;


@ActivityScope
public class SearchResultPresenter extends BasePresenter<SearchResultContract.Model, SearchResultContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private int page=1;
    @Inject
    public SearchResultPresenter(SearchResultContract.Model model, SearchResultContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
    public void loadInitData(){
        page=1;
        SearchResultBean searchResultBean=mRootView.obtainSearchResultBean();
        mModel.getSearchList(page,searchResultBean).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<BaseListData<NewBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<BaseListData<NewBean>> baseListDataBaseData) {
                        page++;
                        mRootView.refreshAdapter(baseListDataBaseData.getData().getList());
                    }
                });
    }
    public void loadMoreData(){
        SearchResultBean searchResultBean=mRootView.obtainSearchResultBean();
        mModel.getSearchList(page,searchResultBean).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<BaseListData<NewBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<BaseListData<NewBean>> baseListDataBaseData) {
                        mRootView.obtainAdapter().loadMoreComplete();
                        mRootView.notifyAddData(baseListDataBaseData.getData().getList());
                        if (baseListDataBaseData.getData().getList().size()< AppConstants.PAGE_NUM){
                            mRootView.obtainAdapter().loadMoreEnd();
                        }
                    }
                });
    }

}
